package com.fand.acceptControl.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.CollectionUtils;

import com.fand.acceptControl.dao.NavigationDao;
import com.fand.acceptControl.dto.NavigationDto;
import com.fand.context.mapper.JsonMapper;
import com.fand.template.utils.FreemarkerTemplateUtils;

/**
 * 导航服务
 * 
 * @author haitao.yao
 * 
 */
public class DefaultNavigationService implements NavigationService {
	private AcceptControlService acceptControlService;

	private NavigationDao navigationDao;

	private void processData(final NavigationDto navigation) {
		String url = navigation.getUrl();

		final Map<String, String> model = this.acceptControlService.getCommonService().getConfigurationService().getVariableService()
				.saveVariableBy1ForMap();

		url = FreemarkerTemplateUtils.processString(url, model);

		navigation.setUrl(url);

		String jsonData = JsonMapper.nonEmptyMapper().toJson(navigation);

		jsonData = StringUtils.replace(jsonData, "'", "`");

		navigation.setJsonData(jsonData);
	}

	@Override
	@CacheEvict(value = NavigationService.CACHE_NAME, allEntries = true)
	public void reload() {
	}

	@Override
	public String saveNavigationBy1ForObject(final NavigationDto navigation) {
		return this.navigationDao.saveNavigationBy1ForObject(navigation);
	}

	@Override
	@Cacheable(value = NavigationService.CACHE_NAME)
	public List<NavigationDto> selectNavigationBy2ForMappedObjectList(final NavigationDto selectNavigationBy) {
		final List<NavigationDto> navigationList = this.navigationDao.selectNavigationBy2ForMappedObjectList(selectNavigationBy);

		if (CollectionUtils.isEmpty(navigationList)) {
			return navigationList;
		}

		for (final NavigationDto navigation : navigationList) {
			this.processData(navigation);
		}

		return navigationList;
	}

	@Override
	@Cacheable(value = NavigationService.CACHE_NAME)
	public List<NavigationDto> selectNavigationBy3ForMappedObjectList(final NavigationDto selectNavigationBy, final int treeDepthNum) {
		final List<NavigationDto> navigationList = this.navigationDao.selectNavigationBy3ForMappedObjectList(selectNavigationBy);

		if (CollectionUtils.isEmpty(navigationList)) {
			return navigationList;
		}

		for (final NavigationDto navigation : navigationList) {
			this.processData(navigation);
		}

		if (treeDepthNum > 1) {
			for (final NavigationDto itemNavigation : navigationList) {
				selectNavigationBy.setParentId(itemNavigation.getId());

				final List<NavigationDto> childNavigationList = this.selectNavigationBy3ForMappedObjectList(selectNavigationBy,
						(treeDepthNum - 1));

				itemNavigation.setChilds(childNavigationList);
			}
		}

		return navigationList;
	}

	public void setAcceptControlService(final AcceptControlService acceptControlService) {
		this.acceptControlService = acceptControlService;
	}

	public void setNavigationDao(final NavigationDao navigationDao) {
		this.navigationDao = navigationDao;
	}

}